<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ProfileController
 *
 * @author Sorasak Siangsanan
 */
class ProfileController extends Controller {

    public function actionIndex() {

        $model = new ProfileForm;
        $model->isChangPassword = false;
        $customer = Customer::model()->findByPk(Yii::app()->user->id);

        // collect user input data
        if (isset($_POST['ProfileForm'])) {
            $model->birthday = $_POST['ProfileForm']['birthday'];
            $model->attributes = $_POST['ProfileForm'];
            $model->time_of_birthday = $_POST['time_of_birthday'];
            $model->password = $_POST['ProfileForm']['password'];
            $model->repassword = $_POST['ProfileForm']['repassword'];

            if (strlen($model->password) > 0 || strlen($model->repassword) > 0) {
                $model->scenario = 'changePassword';
                $model->isChangPassword = true;
            }

            if ($model->validate() && $model->profile($customer)) {
                if ($model->isChangPassword) {
                    $this->redirect(array('/logout'));
                } else {
                    $this->redirect(array('/profile'));
                }
            }

            $model->password = "";
            $model->repassword = "";
        }

        $this->render('index', array('model' => $model, 'customer' => $customer));
    }

}
